package study.itheima.servlet;

import com.github.pagehelper.PageInfo;
import study.itheima.domain.User;
import study.itheima.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
//user类的servlet方法
@WebServlet("/user/*")
public class UserServlet extends BaseServlet {
    private UserService user= new UserService();
    public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String name = req.getParameter("name");
        int age = Integer.parseInt(req.getParameter("age"));
        User user1 = user.findBynameageService(name, age);
        if(user1 != null){
            req.getRequestDispatcher("/user/list").forward(req,resp);
        }else {
            req.getRequestDispatcher("/error.jsp").forward(req,resp);
        }
    }
    public void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("Text/Html;charset=UTF-8");
        String str = req.getParameter("pageNum");
        int pageNum =1;//设置默认访问首页
        if(str!=null){
            pageNum = Integer.parseInt(str);
        }
        List<User> list = user.findAllService(pageNum);
        PageInfo<User> pageInfo = new PageInfo<User>(list);

        req.setAttribute("pageInfo",pageInfo);
        req.getRequestDispatcher("/main.jsp").forward(req,resp);
    }
}
